﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToRTR.Views.CApExportToRTRManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.InterferenceVolumeQuery.Tree;
using EngineeringInternalExtension;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Media.Imaging;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToRTR.Views;

internal class CApExportToRTRManager
{
  private static BitmapImage GetImage(ITxObject obj)
  {
    Image objectIcon = TxIconManager.the().GetObjectIcon(obj);
    if (objectIcon == null)
      return (BitmapImage) null;
    Bitmap bitmap = new Bitmap(objectIcon);
    BitmapImage image = new BitmapImage();
    MemoryStream memoryStream = new MemoryStream();
    bitmap.Save((Stream) memoryStream, ImageFormat.Png);
    image.BeginInit();
    image.StreamSource = (Stream) new MemoryStream(memoryStream.ToArray());
    image.EndInit();
    return image;
  }

  public static List<IExportToRTRTreeViewItem> CreateTreeViewItems(
    TxObjectList objs,
    CUiExportToRTRContentViewModel model)
  {
    List<IExportToRTRTreeViewItem> treeViewItems = new List<IExportToRTRTreeViewItem>();
    foreach (ITxObject itxObject in (Collection<ITxObject>) objs)
    {
      if (itxObject is ITxRoboticOrderedCompoundOperation operation)
      {
        bool flag = false;
        IExportToRTRTreeViewItem toRtrTreeViewItem1 = (IExportToRTRTreeViewItem) new ExportToRTRTreeItem(((ITxObject) operation).Name, (ITxObject) operation, (ObservableCollection<IExportToRTRTreeViewItem>) null, CApExportToRTRManager.GetImage((ITxObject) operation), model.RoadMapConnectionOptions, model?.DataOf((ITxRoboticOperation) operation));
        ITxObjectCollection objectCollection = (ITxObjectCollection) operation;
        if (objectCollection != null)
        {
          foreach (ITxRoboticLocationOperation directDescendant in (Collection<ITxObject>) objectCollection.GetDirectDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxRoboticLocationOperation))))
          {
            flag = true;
            IExportToRTRTreeViewItem toRtrTreeViewItem2 = (IExportToRTRTreeViewItem) new ExportToRTRTreeItem(((ITxObject) directDescendant).Name, (ITxObject) directDescendant, (ObservableCollection<IExportToRTRTreeViewItem>) null, CApExportToRTRManager.GetImage((ITxObject) directDescendant), model.RoadMapConnectionOptions, model?.DataOf((ITxRoboticOperation) directDescendant));
            toRtrTreeViewItem1.Children.Add(toRtrTreeViewItem2);
          }
        }
        if (flag)
          treeViewItems.Add(toRtrTreeViewItem1);
      }
      else if (itxObject is ITxCompoundOperation compoundOperation)
      {
        foreach (IExportToRTRTreeViewItem treeViewItem in CApExportToRTRManager.CreateTreeViewItems(((ITxObjectCollection) compoundOperation).GetDirectDescendants(CUiExportToRTRContentViewModel.GetSupportedOperationFilter()), model))
          treeViewItems.Add(treeViewItem);
      }
    }
    TxApplication.RefreshDisplay();
    return treeViewItems;
  }

  public static void ChangeSelection(TreeNode selectedObjs)
  {
    if ((selectedObjs.Tag as IExportToRTRTreeViewItem).ExportToRTRObject == null)
      return;
    ITxObject exportToRtrObject = (selectedObjs.Tag as IExportToRTRTreeViewItem).ExportToRTRObject;
    TxSelection selection = TxApplication.ActiveDocument.Selection;
    TxObjectList txObjectList = new TxObjectList();
    ((Collection<ITxObject>) txObjectList).Add(exportToRtrObject);
    selection.SetItems(txObjectList);
  }
}
